package cn.tedu.controller;

import cn.tedu.entity.CartItem;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @author JokerQiu
 * @date 2021年12月22日 20:35
 */
@WebServlet("/changeAmount")
public class ChangeAmountServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("变更数量");
        // 获取参数
        String bookId = req.getParameter("bookId");
        String amount = req.getParameter("amount");
        System.out.println(bookId + ":" + amount);
        // 获取购物车里面的商品，并修改数量
        HttpSession session = req.getSession();
        Map<String, CartItem> cart = (Map<String, CartItem>) session.getAttribute("cart");
        CartItem cartItem = cart.get(bookId);
        cartItem.setAmount(Integer.parseInt(amount));
        resp.getWriter().write("变更数量成功");
    }
}
